using HousingInfo.DataLinQ;

using System;
using System.Linq;
using Csla;
using Csla.Data;
using Csla.Security;
using Csla.Validation;

namespace HousingInfo.Library
{ 
	[Serializable()] 
	public partial class VipHouseBO : Csla.BusinessBase<VipHouseBO>
	{
		#region Business Properties and Methods
		//register properties
		private static PropertyInfo<int> VipHouseIDProperty = RegisterProperty<int>(typeof(VipHouseBO), new PropertyInfo<int>("VipHouseID"));
		private static PropertyInfo<int?> NewsHouseIDProperty = RegisterProperty<int?>(typeof(VipHouseBO), new PropertyInfo<int?>("NewsHouseID"));
		private static PropertyInfo<decimal?> PriceProperty = RegisterProperty<decimal?>(typeof(VipHouseBO), new PropertyInfo<decimal?>("Price"));
		private static PropertyInfo<DateTime?> RegisterDateProperty = RegisterProperty<DateTime?>(typeof(VipHouseBO), new PropertyInfo<DateTime?>("RegisterDate"));
		private static PropertyInfo<int?> NumRegisterProperty = RegisterProperty<int?>(typeof(VipHouseBO), new PropertyInfo<int?>("NumRegister"));
		private static PropertyInfo<bool> IsActiveProperty = RegisterProperty<bool>(typeof(VipHouseBO), new PropertyInfo<bool>("IsActive"));
		private static PropertyInfo<int?> NumPostProperty = RegisterProperty<int?>(typeof(VipHouseBO), new PropertyInfo<int?>("NumPost"));
		private static PropertyInfo<bool> IsExtensionProperty = RegisterProperty<bool>(typeof(VipHouseBO), new PropertyInfo<bool>("IsExtension"));

		[System.ComponentModel.DataObjectField(true, true)]
		public int VipHouseID
		{
			get { return GetProperty<int>(VipHouseIDProperty); }
		}
	 
		public int? NewsHouseID
		{
			get { return GetProperty<int?>(NewsHouseIDProperty); }
			set { SetProperty<int?>(NewsHouseIDProperty, value); }
		}
	 
		public decimal? Price
		{
			get { return GetProperty<decimal?>(PriceProperty); }
			set { SetProperty<decimal?>(PriceProperty, value); }
		}
	 
		public DateTime? RegisterDate
		{
			get { return GetProperty<DateTime?>(RegisterDateProperty); }
			set { SetProperty<DateTime?>(RegisterDateProperty, value); }
		}
	 
		public int? NumRegister
		{
			get { return GetProperty<int?>(NumRegisterProperty); }
			set { SetProperty<int?>(NumRegisterProperty, value); }
		}
	 
		public bool IsActive
		{
			get { return GetProperty<bool>(IsActiveProperty); }
			set { SetProperty<bool>(IsActiveProperty, value); }
		}
	 
		public int? NumPost
		{
			get { return GetProperty<int?>(NumPostProperty); }
			set { SetProperty<int?>(NumPostProperty, value); }
		}
	 
		public bool IsExtension
		{
			get { return GetProperty<bool>(IsExtensionProperty); }
			set { SetProperty<bool>(IsExtensionProperty, value); }
		}
	 
		#endregion //Business Properties and Methods

		#region Validation Rules
		protected override void AddBusinessRules()
		{


			AddCustomRules();
		}

		partial void AddCustomRules();
		#endregion //Validation Rules

		#region Factory Methods
		public static VipHouseBO NewVipHouseBO()
		{
			return DataPortal.Create<VipHouseBO>();
		}

		public static VipHouseBO GetVipHouseBO(int vipHouseID)
		{
			return DataPortal.Fetch<VipHouseBO>(new SingleCriteria<VipHouseBO, int>(vipHouseID));
		}

		public static void DeleteVipHouseBO(int vipHouseID)
		{
			DataPortal.Delete(new SingleCriteria<VipHouseBO, int>(vipHouseID));
		}

		private VipHouseBO()
		{ /* require use of factory method */ }

		public VipHouseBO CopyVipHouseBO()
		{
			VipHouseBO objectCopy = this.Clone();
			objectCopy.MarkNew();
			return objectCopy;
		}
		#endregion //Factory Methods


		#region Data Access

		#region Data Access - Create
		[RunLocal]
		protected override void DataPortal_Create()
		{
			bool cancel = false;
			OnCreating(ref cancel);
			if (cancel) return;

			ValidationRules.CheckRules();

			//OnCreated();
		}

		partial void OnCreating(ref bool cancel);
		//partial void OnCreated();
		#endregion //Data Access - Create

		#region Data Access - Fetch
		private void DataPortal_Fetch(SingleCriteria<VipHouseBO, int> criteria)
		{
			bool cancel = false;
			OnFetching(criteria, ref cancel);
			if (cancel) return;

			using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
						.GetManager(Database.HousingInfoConnection))
			{
				var data = mgr.DataContext.VipHouses
						.Single(r => r.VipHouseID == criteria.Value);

				OnMemberLoading(data);
				LoadProperty<int>(VipHouseIDProperty, data.VipHouseID);
				LoadProperty<int?>(NewsHouseIDProperty, data.NewsHouseID);
				LoadProperty<decimal?>(PriceProperty, data.Price);
				LoadProperty<DateTime?>(RegisterDateProperty, data.RegisterDate);
				LoadProperty<int?>(NumRegisterProperty, data.NumRegister);
				LoadProperty<bool>(IsActiveProperty, data.IsActive);
				LoadProperty<int?>(NumPostProperty, data.NumPost);
				LoadProperty<bool>(IsExtensionProperty, data.IsExtension);
				//OnMemberLoaded();

				ValidationRules.CheckRules();
			}//using

			//OnFetched();
		}

		partial void OnFetching(SingleCriteria<VipHouseBO, int> criteria, ref bool cancel);
		//partial void OnFetched();
		partial void OnMemberLoading(HousingInfo.DataLinQ.VipHouse data);
		//partial void OnMemberLoaded();
		#endregion //Data Access - Fetch

		#region Data Access - Insert
		protected override void DataPortal_Insert()
		{
			bool cancel = false;
			OnInserting(ref cancel);
			if (cancel) return;

			using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
						.GetManager(Database.HousingInfoConnection))
			{
				var data = new HousingInfo.DataLinQ.VipHouse();

				OnMemberReading(data);

				data.NewsHouseID = ReadProperty<int?>(NewsHouseIDProperty).Value;
				data.Price = ReadProperty<decimal?>(PriceProperty).Value;
				data.RegisterDate = ReadProperty<DateTime?>(RegisterDateProperty).Value;
				data.NumRegister = ReadProperty<int?>(NumRegisterProperty).Value;
				data.IsActive = ReadProperty<bool>(IsActiveProperty);
				data.NumPost = ReadProperty<int?>(NumPostProperty).Value;
				data.IsExtension = ReadProperty<bool>(IsExtensionProperty);
				AssignParentKey(data);
				//OnMemberRead();

				mgr.DataContext.VipHouses.InsertOnSubmit(data);
				mgr.DataContext.SubmitChanges();

				LoadProperty<int>(VipHouseIDProperty, data.VipHouseID);
			}//using

			//OnInserted();
		}

		partial void OnInserting(ref bool cancel);
		//partial void OnInserted();
		partial void OnMemberReading(HousingInfo.DataLinQ.VipHouse data);
		//partial void OnMemberRead();
		#endregion //Data Access - Insert

		#region Data Access - Update
		protected override void DataPortal_Update()
		{
			bool cancel = false;
			OnUpdating(ref cancel);
			if (cancel) return;

			using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
						.GetManager(Database.HousingInfoConnection))
			{
				var data = new HousingInfo.DataLinQ.VipHouse() 
				{
					VipHouseID = ReadProperty<int>(VipHouseIDProperty)
				};
				mgr.DataContext.VipHouses.Attach(data);

				OnMemberReading(data);
				if (IsSelfDirty)
				{
					data.NewsHouseID = ReadProperty<int?>(NewsHouseIDProperty).Value;
					data.Price = ReadProperty<decimal?>(PriceProperty).Value;
					data.RegisterDate = ReadProperty<DateTime?>(RegisterDateProperty).Value;
					data.NumRegister = ReadProperty<int?>(NumRegisterProperty).Value;
					data.IsActive = ReadProperty<bool>(IsActiveProperty);
					data.NumPost = ReadProperty<int?>(NumPostProperty).Value;
					data.IsExtension = ReadProperty<bool>(IsExtensionProperty);
				}
				//OnMemberRead();

				mgr.DataContext.SubmitChanges();
				if (IsSelfDirty)
				{
					LoadProperty<int>(VipHouseIDProperty, data.VipHouseID);
				}
			}//using

			//OnUpdated();
		}
		partial void OnUpdating(ref bool cancel);
		//partial void OnUpdated();
		#endregion //Data Access - Update


		#region Data Access - Delete
		protected override void DataPortal_DeleteSelf()
		{
			bool cancel = false;
			OnSelfDeleting(ref cancel);
			if (cancel) return;

			using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
						.GetManager(Database.HousingInfoConnection))
			{
				OnMemberSelfDeleting();

				var data = new HousingInfo.DataLinQ.VipHouse() 
				{
					VipHouseID = ReadProperty<int>(VipHouseIDProperty)
				};
				mgr.DataContext.VipHouses.Attach(data);

				mgr.DataContext.VipHouses.DeleteOnSubmit(data);
				OnMemberSelfDeleted();

				mgr.DataContext.SubmitChanges();
			}//using

			//OnSelfDeleted();
		}

		partial void OnSelfDeleting(ref bool cancel);
		//partial void OnSelfDeleted();
		partial void OnMemberSelfDeleting();
		partial void OnMemberSelfDeleted();

		private void DataPortal_Delete(SingleCriteria<VipHouseBO, int> criteria)
		{
			bool cancel = false;
			OnDeleting(criteria, ref cancel);
			if (cancel) return;

			using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
						.GetManager(Database.HousingInfoConnection))
			{
				var data = mgr.DataContext.VipHouses
						.Single(r => r.VipHouseID == criteria.Value);

				OnMemberDeleting(data);

				mgr.DataContext.VipHouses.DeleteOnSubmit(data);
				OnMemberDeleted();

				mgr.DataContext.SubmitChanges();
			}//using

			OnDeleted();
		}

		partial void OnDeleting(SingleCriteria<VipHouseBO, int> criteria, ref bool cancel);
		partial void OnDeleted();
		partial void OnMemberDeleting(HousingInfo.DataLinQ.VipHouse data);
		partial void OnMemberDeleted();
		#endregion //Data Access - Delete
		#endregion //Data Access

	}
}
